package com.htht.smartschool.service.askforleave;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.htht.smartschool.common.service.BaseService;
import com.htht.smartschool.model.askforleave.TLeave;
import com.htht.smartschool.model.teacher.TClass;

public class LeaveService extends BaseService {
	public TLeave add(TLeave tleave){
		return (TLeave) getDao().create(tleave);
	}
	public TLeave update(TLeave tLeave){
		return (TLeave) getDao().update(tLeave);
	}
	public boolean delete(TLeave tLeave){
		boolean resultFlag = false;
		Long id = tLeave.getId();
		String flag = tLeave.getFlag();
		int i = getDao().executeSQL("update t_leave set flag = '"+flag+"' where id="+id+"");
		if(i>=0){
			resultFlag = true;
		}else{
			resultFlag = false;
		}
		return resultFlag;
	}
	@SuppressWarnings("unchecked")
	public List<TLeave> select(TLeave tleave){
		String studentcode = tleave.getStudentcode();
		String hql = "from TLeave where studentcode = '"+studentcode+"' ";
		if(tleave.getStarttime()!=null){
			hql +=" and starttime >= '"+tleave.getStarttime()+"' ";
		}
		if(tleave.getEndtime()!=null){
			hql += " and starttime <='"+tleave.getEndtime()+"'";
		}
		if(tleave.getFlag()!=null){
			String[] flagArray = tleave.getFlag().split(",");
			if(flagArray.length>1){
				tleave.setFlag(tleave.getFlag().replace(",", "^"));
				hql += " and flag in (:flag)";
			}else{
				hql += " and flag = :flag";
			}
		}
		hql += " ORDER BY createtime desc";
		List<TLeave> list = getDao().queryByHQL(hql,tleave);
		return list;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map selectBypage(TLeave tleave){
		String hql = "select ta,ts.name,tc from TLeave ta ,TStudent ts,TClass tc where ts.classcode = tc.classcode and ta.studentcode = ts.studentcode";
		Map<String,Object> param = new HashMap<String, Object>();
		if(tleave.getStarttime()!=null){
			hql +=" and ta.starttime >=:starttime";
			param.put("starttime",tleave.getStarttime());
		}
		if(tleave.getEndtime()!=null){
			hql += " and ta.endtime <=:endtime";
			param.put("endtime",tleave.getEndtime());
		}
		if(tleave.getStudentcode()!=null && tleave.getStudentcode().length() > 0){
			hql += " and ta.studentcode =:studentcode";
			param.put("studentcode",tleave.getStudentcode());
		}
		if(tleave.getClassinfo() != null && tleave.getClassinfo().getClass_() != null && tleave.getClassinfo().getClass_() > 0){
			hql += " and tc.class =:class";
			param.put("class",tleave.getClassinfo().getClass_());
		}
		if(tleave.getClassinfo() != null && tleave.getClassinfo().getGrade() != null && tleave.getClassinfo().getGrade() > 0){
			hql += " and tc.grade =:grade";
			param.put("grade",tleave.getClassinfo().getGrade());
		}
		if(tleave.getClassinfo() != null && tleave.getClassinfo().getType() != null && tleave.getClassinfo().getType().length() > 0){
			hql += " and tc.type =:type";
			param.put("type",tleave.getClassinfo().getType());
		}
		if(tleave.getClassinfo() != null && tleave.getClassinfo().getSchoolcode() != null && tleave.getClassinfo().getSchoolcode().length() > 0){
			hql += " and tc.schoolcode = :schoolcode";
			param.put("schoolcode",tleave.getClassinfo().getSchoolcode());
		}
		if(tleave.getFlag()!=null){
			String[] flagArray = tleave.getFlag().split(",");
			if(flagArray.length>1){
				tleave.setFlag(tleave.getFlag().replace(",", "^"));
				hql += " and ta.flag in (:flag)";
			}else{
				hql += " and ta.flag =:flag";
			}
			param.put("flag",tleave.getFlag());
		}
		hql += " ORDER BY ta.starttime desc";
		List<Object[]> list = getDao().queryByHQLPage(hql,param, tleave.getCurrentpage(), tleave.getPagesize());
		List<TLeave> result = new ArrayList<TLeave>();
		for (Object[] obj : list) {
			TLeave tl = (TLeave) obj[0];
			tl.setName(String.valueOf(obj[1]));
			tl.setClassinfo((TClass)obj[2]);
			result.add(tl);
		}
		int count = getDao().getCountByHqlPage(hql,param);
		Map map = new HashMap();
		map.put("data", result);
		map.put("total", count);
		return map;
	}
	public boolean agreeLeave(Long id,String flag,String remark) {
		//业务不进行批量修改 暂时注释
//		String[] idStrings = ids.split(",");
//		String id = "(";
//		for (int i = 0; i < idStrings.length; i++) {
//			id += "'"+idStrings[i]+"'";
//		}
		Long time = new Date().getTime()/1000;
		String sql = "UPDATE t_leave ts SET ts.flag = '"+flag+"' , ts.agreetime ='"+time+"' , ts.remark ='"+remark+"' WHERE id = "+id+" ";
		int a = getDao().executeSQL(sql);
		if(a == -1){
			return false;
		}else{
			return true;
		}
		
	}
	public List<TLeave> teacherGetLeave(TLeave tleave) {
		tleave.setCurrentpage(tleave.getCurrentpage() == null ? 0 :tleave.getCurrentpage());
		tleave.setPagesize(tleave.getPagesize() == null ? 0 :tleave.getPagesize());
		String sql = "SELECT tl.id,tl.studentcode,tl.starttime,tl.endtime,tl.createtime,IFNULL(tl.agreetime,0),tl.reason,tl.flag,tl.type,tl.remark,s.name"
				+ " FROM t_leave tl,(SELECT ts.name,ts.studentcode FROM t_student ts WHERE ts.classcode = '"+tleave.getClasscode()+"') s WHERE tl.studentcode = s.studentcode";
		if(tleave.getStarttime()!=null){
			sql +=" and tl.starttime >= :starttime and tl.starttime <= :endtime ";
		}
		if(tleave.getEndtime()!=null){
			sql +=" and tl.endtime >= :starttime and tl.endtime <=:endtime ";
		}
		if(tleave.getType() != null && tleave.getType().length() >0){
			sql +=" and tl.type <= :type ";
		}
		if(tleave.getStudentcode() != null && tleave.getStudentcode().length() >0){
			sql +=" and tl.studentcode = :studentcode ";
		}
		//开放查询各种审批状态接口
		if(tleave.getFlag()!=null){
			String[] flagArray = tleave.getFlag().split(",");
			if(flagArray.length>1){
				tleave.setFlag(tleave.getFlag().replace(",", "^"));
				sql += " and flag in (:flag)";
			}else{
				sql += " and flag = :flag";
			}
		}
		sql += " ORDER BY createtime desc,starttime desc";
		List<Object[]> list = new ArrayList<Object[]>();
		int counts = 0;
		if(tleave.getCurrentpage() == 0 && tleave.getPagesize() == 0){
			list = getDao().queryBySQL(sql,tleave);
		}else{
			list = getDao().queryBySqlPage(sql,tleave, tleave.getCurrentpage(), tleave.getPagesize());
			String sql2 = "SELECT COUNT(*) FROM (" + sql + ") c";
			counts = getDao().getCountBySqlPage(sql2,tleave);
		}
		List<TLeave> result = new ArrayList<TLeave>();
		for (Object[] obj : list) {
			TLeave tl = new TLeave();
			tl.setId(Long.valueOf(String.valueOf(obj[0])));
			tl.setStudentcode(String.valueOf(obj[1]));
			tl.setStarttime(Long.valueOf(String.valueOf(obj[2])));
			tl.setEndtime(Long.valueOf(String.valueOf(obj[3])));
			tl.setCreatetime(Long.valueOf(String.valueOf(obj[4])));
			tl.setAgreetime(Long.valueOf(String.valueOf(obj[5])));
			tl.setReason(String.valueOf(obj[6]));
			tl.setFlag(String.valueOf(obj[7]));
			tl.setType(String.valueOf(obj[8]));
			tl.setRemark(String.valueOf(obj[9]));
			tl.setName(String.valueOf(obj[10]));
			tl.setTotal((long) counts);
			result.add(tl);
		}
		return result;
	}
}
